package data_reader;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TimeReader {
	private static final String URL       = "http://www.ratt.ro/txt/";
	private static final String proxyHost = "10.14.1.1";
    private static final String proxyPort = "3128";
    
    public void setNetworkSettings()
    {
    	if (!proxyHost.equals("") && !proxyPort.equals(""))
    	{
        	System.setProperty("http.proxyHost", proxyHost);
        	System.setProperty("http.proxyPort", proxyPort);
    	}
    }

	public ArrayList<Calendar> getTimes(String lineId, String stationId )
	{
		URL url;
		InputStream timeInfo = null;
		ArrayList<Calendar> times = null;
//		ArrayList<Calendar> times = new ArrayList<Calendar>();
//		Calendar timeOfUpdate = Calendar.getInstance();
//		timeOfUpdate.setTimeInMillis(System.currentTimeMillis());
//		times.add(timeOfUpdate);
		try
		{
			url = new URL(URL+"afis_msg.php"+"?"+"id_traseu"+"="+lineId+"&"+"id_statie"+"="+stationId);
		    timeInfo = url.openStream();
			@SuppressWarnings("resource")
			Scanner s = new Scanner(timeInfo).useDelimiter("\\A");
			if (s.hasNext())
			{
				times = new ArrayList<Calendar>();
				for (String t: formatTimes(s.next()))
				{
					times.add(formatTime(t));
				}
				if (times.get(0) == null || times.get(1) == null)
				{
					return null;
				}
			}
			return times;	
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}	
		return null;  
	}
	
	private ArrayList<String> formatTimes(String info)
	{
		ArrayList<String> results = null;
		Pattern infoPattern = Pattern.compile(".*Sosire1:\\s*(.*)<br>.*Sosire2:\\s*(.*)</b>.*");
		Matcher infoMatcher = infoPattern.matcher(info);
		while (infoMatcher.find())
		{
		 results = new ArrayList<String>();
		 results.add(infoMatcher.group(1)); 
		 results.add(infoMatcher.group(2));
		}
		return results;
	}
	
	private Calendar formatTime(String time)
	{
		Calendar arrivalTime = Calendar.getInstance(TimeZone.getTimeZone("Europe/Bucharest"));
		time = time.trim();
		String minString = "";
		if (time.endsWith("min"))	
		{
			minString = time.substring(0, time.length()-3).trim();
			arrivalTime.add(Calendar.MINUTE, Integer.parseInt(minString));
			return arrivalTime;
		}
		else if(time.endsWith("min."))
		{
			minString = time.substring(0, time.length()-4).trim();
			arrivalTime.add(Calendar.MINUTE, Integer.parseInt(minString));
			return arrivalTime;
		}
        else if (time.equals(".."))
        {
			return null;
		}
        else if (time.equals(">>"))
        {
			return arrivalTime;
		} 
        else if (time.length()>=5 && time.charAt(2)==':')
        {
			String hourString = time.substring(0,2);
		    minString = time.substring(3);
		    if (!hourString.equals("**") && !minString.equals("**"))
		    {
		    	arrivalTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourString));
		    	arrivalTime.set(Calendar.MINUTE, Integer.parseInt(minString));
		    	return arrivalTime;
		    }
		    else
		    {
		    	return null;
		    }
			
		} 
        else
        {
			return null;
        }
	}
}
